﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Mvc;

namespace DFGJ.WMS_PDA.WebAPI.Controllers.PDA
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PDASelectOutboundController : Controller
    {
        private IPDASelectOutboundRepository repository;
        private ITOutboundOrderDetailRepository TOutboundOrderDetail;

        public PDASelectOutboundController(IPDASelectOutboundRepository repository, ITOutboundOrderDetailRepository tOutboundOrderDetail)
        {
            this.repository = repository;
            TOutboundOrderDetail = tOutboundOrderDetail;
        }

        /// <summary>
        /// 拣选出库
        /// </summary>
        /// <param name="PalletBarcode"></param>
        /// <returns></returns>
        //[HttpPost]
        //public IActionResult GetSelectShipping(SubmitOutListDTO orderDTO)
        //{
        //    try
        //    {
        //        string msg = "";
        //        var IsOk = repository.GetSelectShipping(orderDTO.palletBarcode[0],orderDTO.qty, out msg);
        //        if (!IsOk)
        //        {
        //            return Ok(BaseResult.Result(false, msg, "500"));
        //        }

        //        bool result = false;
        //        result = TOutboundOrderDetail.SubmitOutInventoryReview(orderDTO, out msg);

        //        if (!result)
        //        {
        //            //transaction.Dispose();
        //            return Ok(BaseResult.Result("500", msg));
        //        }

        //        if (TOutboundOrderDetail.GetStackingStorages(orderDTO.palletBarcode).Count <= 0)
        //        {
        //            return Ok(BaseResult.Result("500", "请求参数有误，请检查请求参数"));
        //        }

        //        //根据垛条码查询垛主
        //        var item = TOutboundOrderDetail.GetStackingStacker(orderDTO.palletBarcode);
        //        if (item.Count == 0)
        //        {
        //            //transaction.Dispose();
        //            return Ok(BaseResult.Result("500", "该托条码对应的垛主信息有误"));
        //        }

        //        // 根据垛条码删除入库垛信息表中数据
        //        if (!TOutboundOrderDetail.HandleUnbindOfOutstorage(orderDTO, out msg))
        //        {
        //            //transaction.Dispose();
        //            return Ok(BaseResult.Result("500", msg));
        //        }
        //        return Ok(BaseResult.Result("200", "出库解绑成功"));
        //    }
        //    catch (Exception ex)
        //    {
        //        //transaction.Dispose(); // 或者使用 tran.Rollback()
        //        return Ok(BaseResult.Result("500", ex.Message));
        //    }
        //}

        /// <summary>
        /// 拣选出库
        /// </summary>
        /// <param name="PalletBarcode"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult GetSelectShipping(SelectOutInventoryDto orderDTO)
        {
            try
            {
                string msg = "";
                var IsOk = repository.GetSelectShipping(orderDTO.PalletBarcode,orderDTO.ShippingOrderSn,/*orderDTO.Qty,*/  out msg);
                if (!IsOk)
                {
                    return Ok(BaseResult.Result(false, msg, "500"));
                }
                return Ok(BaseResult.Result(true, "拣选成功", "200"));
            }
            catch (Exception ex)
            {
                //transaction.Dispose(); // 或者使用 tran.Rollback()
                return Ok(BaseResult.Result("500", ex.Message));
            }
        }

        /// <summary>
        /// 满载率
        /// </summary>
        /// <param name="PalletBarcode"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPallLoadFactor(string PalletBarcode)
        {
            string msg = "";
            var LoadFactor = repository.GetPallLoadFactor(PalletBarcode, out msg);
            if (LoadFactor != 0 && LoadFactor != null)
            {
                return Ok(BaseResult.Result(LoadFactor, "查询成功！", "200"));
            }
            return Ok(BaseResult.Result(0, msg, "500"));
        }


        /// <summary>
        /// 根据托盘条码带出相关信息
        /// </summary>
        /// <param name="PalletBarcode"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInformationByBarcode(string PalletBarcode)
        {
            string msg = "";
            var LoadFactor = repository.GetInformationByBarcode(PalletBarcode, out msg);
            if (LoadFactor != null)
            {
                return Ok(BaseResult.Result(LoadFactor, "查询成功！", "200"));
            }
            return Ok(BaseResult.Result(0, msg, "500"));
        }

    }
}
